﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Travianer.Controller;
using Travianer.Model;
using System.Threading;

namespace Travianer
{
    public partial class MainForm : Form
    {
        public MainForm(string data)
        {
            InitializeComponent();
            Load(data);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.ExitThread();
        }

        /// <summary>
        /// 加载Form数据
        /// </summary>
        /// <param name="data"></param>
        private void Load(string data)
        {
            //village1
            GetVillage(data);
            GetResources(data);
            GetVillage1(data);
            GetTroops(data);
            //village2
            var entVillage2 = HandleController.GetInstance().GetVillage2(string.Empty);
            GetVillage2(entVillage2.SourceCode);
        }

        /// <summary>
        /// 获取村庄数据
        /// </summary>
        /// <param name="data"></param>
        private void GetVillage(string data)
        {
            var lst = DataController.GetInstance().GetVillageList(data);
            lbVillage.Items.Clear();
            foreach (var ent in lst)
            {
                lbVillage.Items.Add(ent.VillageName + "|" + ent.VID);
                if (ent.IsActive)
                {
                    lbVillage.SelectedIndex = lbVillage.Items.Count - 1;
                    lblCurrentVillage.Text = ent.VillageName;
                }
            }
        }

        /// <summary>
        /// 获取资源数据
        /// </summary>
        /// <param name="data"></param>
        private void GetResources(string data)
        {
            var ent = DataController.GetInstance().GetVillageInfomation(data);
            lblWood.Text = ent.Wood + " / " + ent.Warehourse;
            lblClay.Text = ent.Clay + " / " + ent.Warehourse;
            lblIron.Text = ent.Iron + " / " + ent.Warehourse;
            lblCrop.Text = ent.Crop + " / " + ent.Granary;
        }

        /// <summary>
        /// 获取资源田数据
        /// </summary>
        /// <param name="data"></param>
        private void GetVillage1(string data)
        {
            dgvVillage1.DataSource = DataController.GetInstance().GetVillage1Infomation(data);
        }

        /// <summary>
        /// 获取内城数据
        /// </summary>
        /// <param name="data"></param>
        private void GetVillage2(string data)
        {
            dgvVillage2.DataSource = DataController.GetInstance().GetVillage2Infomation(data);
        }

        /// <summary>
        /// 获取驻兵数据
        /// </summary>
        /// <param name="data"></param>
        private void GetTroops(string data)
        {
            dgvTroop.DataSource = DataController.GetInstance().GetTroopInfomation(data);
        }

        /// <summary>
        /// 设置Form状态信息
        /// </summary>
        private void SetStatus()
        {

        }

        #region 自动普田

        /// <summary>
        /// 自动普田-加入列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void res_btnAdd_Click(object sender, EventArgs e)
        {
            string strResCategory;
            if (res_rdoNormal.Checked)
                strResCategory = "普田";
            else if (res_rdoInt.Checked)
                strResCategory = "智能普田";
            else if (res_rdoCrop.Checked)
                strResCategory = "15T";
            else
                strResCategory = "普田";
            AutoResController.GetInstance().Add(new AutoResInfo()
            {
                VID = lbVillage.SelectedItem == null ? string.Empty : lbVillage.SelectedItem.ToString().Split('|')[1],
                Category = strResCategory
            });
            Bindres_dgvRes();

        }

        /// <summary>
        /// 自动普田-开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void res_btnStart_Click(object sender, EventArgs e)
        {
            res_timer.Interval = Convert.ToInt32(res_txtInterval.Text.Trim());
            res_timer.Start();
            this.SetStatus();
        }

        /// <summary>
        /// 自动普田-停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void res_btnStop_Click(object sender, EventArgs e)
        {
            res_timer.Stop();
            this.SetStatus();
        }

        /// <summary>
        /// 自动普田-计时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void res_timer_Tick(object sender, EventArgs e)
        {
            if (ApplicationHelper.htThread.ContainsKey("res"))
                return;
            Thread thread = new Thread(new ThreadStart(DoRes));
            thread.Start();
            ApplicationHelper.htThread.Add("res", thread);
        }

        private void DoRes()
        {
            var lst = AutoResController.GetInstance().GetEntityList(string.Empty);
            foreach (var ent in lst)
            {
                //取资源数据
                var entVillage1 = HandleController.GetInstance().GetVillage1(ent.VID);
                //根据普田类型计算待升级的资源田ID
                int intResID = DataController.GetInstance().GetResIDByCategory(entVillage1.SourceCode, ent.Category);
                //获取验证码
                var entYZCode = HandleController.GetInstance().GetYZCodeForm(ent.VID);
                string strYZCode = DataController.GetInstance().GetYZCode(entYZCode.SourceCode);
                //升级资源田
                HandleController.GetInstance().DoRes(ent.VID, intResID, strYZCode);
            }
            ApplicationHelper.htThread.Remove("res");
        }

        private void Bindres_dgvRes()
        {
            res_dgvRes.DataSource = AutoResController.GetInstance().GetEntityList(string.Empty);
        }

        #endregion

        #region 内城建造

        private void build_btnAdd_Click(object sender, EventArgs e)
        {

        }

        #endregion

        private void tsmiDeleteSelectedRows_Click(object sender, EventArgs e)
        {
            MessageBox.Show(cmsRightClickMenu.SourceControl.Name);
        }
    }
}
